package pharmacy.model;

import model.FavoritableEntity;
import model.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import pharmacy.Pharmacy;

import java.io.IOException;

import static misc.Utils.DATA_FOLDER;

/**
 * Created with IntelliJ IDEA.
 * User: nuno-rafael-figueiredo
 * Date: 04-03-2014
 * Time: 17:39
 */
public class Districts extends FavoritableEntity<String, District> {
    public Districts() {
        super(District[].class, District.class);
    }

    @Override
    protected void parse() {
        try {
            Map<String, District> districts = Map.create();

            Document document = Jsoup.connect(Pharmacy.URL).get();
            final Elements links = document.select("#linksDistritos a");
            for (Element link : links)
                add(new District(link));

            DATA_FOLDER.toJson("distritos", districts);
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    }
}
